package org.jeecg.modules.commodity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.commodity.entity.CommodityType;
import org.jeecg.modules.commodity.entity.Relationship;
import org.jeecg.modules.commodity.mapper.RelationShipMapper;
import org.jeecg.modules.commodity.model.RelationshipModel;
import org.jeecg.modules.commodity.service.CommodityTypeService;
import org.jeecg.modules.commodity.service.RelationShipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Auther: zhuzhuhong
 * @Date: 2019/11/14 10:02
 * @Description:
 */
@Service
public class RelationShipServiceImpl
        extends ServiceImpl<RelationShipMapper, Relationship>
        implements RelationShipService {
    @Autowired
    private RelationShipMapper relationShipMapper;
    @Autowired
    private CommodityTypeService commodityTypeService;

    @Override
    public List<RelationshipModel> findRelationShipModelByPage(String typeId) {
        return relationShipMapper.selectRelationshipModelByTypeId(typeId);
    }

    @Override
    public boolean saveRelationShip(Map<String, String> paramerMap) {
        try {
            //处理参数
            String typeId = paramerMap.get("typeId");
            String filterelementIds = paramerMap.get("filterelementIds");
            //已经选择的滤芯id
            String[] split = filterelementIds.split(",");

            //查询当前商品类型已经配置的滤芯数量,从relationship表中获取
            QueryWrapper<Relationship> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("type_id", typeId);
            int count = this.count(queryWrapper);
            //查询当前商品类型滤芯规格，从commodity_type表中获取数据
            CommodityType commodityType = commodityTypeService.getById(typeId);
            //滤芯规格
            Integer specification = commodityType.getSpecification();

            //判断“已选择+已配置的”是否大于“规格”
            if (split.length + count > specification) {
                //配置失败
                return false;
            } else {
                //配置成功，新增relationship表数据
                //1、创建一个list类型的参数
                List<Relationship> relationshipList = new ArrayList<>(split.length);
                //2、取出当前登录人的信息，用于赋值给create_by和update_by
                LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
                //封装relationship对象到list
                for (int i = 0; i < split.length; i++) {
                    Relationship relationship = Relationship.builder()
                            .relationshipId(UUIDGenerator.generate())
                            .createBy(user.getId())
                            .filterelementId(split[i])
                            .typeId(typeId)
                            .updateBy(user.getId())
                            .build();
                    relationshipList.add(relationship);
                }
                //写入数据库
                int result = relationShipMapper.insertRelationShip(relationshipList);
                if (result > 0) {
                    return true;
                } else {
                    return false;
                }
            }
        } catch (Exception e) {
//            return false;
            throw e;
        }
    }
}
